package com.tenentia.pcmps.swm.data.implementation;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.tenentia.pcmps.swm.Constants;
import com.tenentia.pcmps.swm.data.DataAccessException;
import com.tenentia.pcmps.swm.data.beans.WayType;
import com.tenentia.pcmps.swm.data.interfaces.WayTypesDAO;
import com.tenentia.pcmps.swm.data.util.IBatisUtilities;

/**
 * 
 * @author tEnEntia, S.L.N.E.
 * 
 */
public class WayTypesDAOImpl implements WayTypesDAO {

	private static final Logger LOGGER = Logger.getLogger(WayTypesDAOImpl.class.getPackage().getName());
	
	public WayTypesDAOImpl() {
		super();
	}

	@Override
	public WayType get(WayType wayType) throws DataAccessException {
		try {
			wayType = (WayType) IBatisUtilities.getSqlMapClient().queryForObject("getWayType", wayType.getInternalID());
			LOGGER.debug(wayType);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}
		return wayType;
	}

	@Override
	public List<WayType> getAvailableWayTypes() throws DataAccessException {
		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		List<WayType> methodResult;

		try {
			List<?> tmpList = IBatisUtilities.getSqlMapClient().queryForList("getAvailableWayTypes");

			if (null == tmpList || tmpList.size() == 0) {
				LOGGER.debug("No results...");
				methodResult = new ArrayList<WayType>();
			} else {
				methodResult = new ArrayList<WayType>();
				for (int i = 0; i < tmpList.size(); i++) {
					WayType tmpWayType = (WayType) tmpList.get(i);
					methodResult.add(tmpWayType);
					LOGGER.debug("tmpWayType: " + tmpWayType);
				}
			}
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}

		return methodResult;
	}

}
